package com.beidouapp.esb.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.apache.cxf.message.Message;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;

public class WSServerImpl implements WSServer
{
	private static JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();

	public void setResourceProvider(List<?> serviceObj)
	{
		List<Class<?>> clazzArr = new ArrayList<Class<?>>();
		for (Object object : serviceObj)
		{
		    List<Object> providerList = new ArrayList<Object>();
			providerList.add(new JacksonJsonProvider());
			sf.setProviders(providerList);
			sf.setResourceProvider(object.getClass(), new SingletonResourceProvider(object));
			clazzArr.add(object.getClass());
		}
		sf.setResourceClasses(clazzArr);
	}
	
	public void setInInterceptors(List<Interceptor<? extends Message>> interceptors) {
		sf.setInInterceptors(interceptors);
	}
	
	public void setOutInterceptors(List<Interceptor<? extends Message>> interceptors) {
		sf.setOutInterceptors(interceptors);
	}
	
//	public void setOutFaultInterceptors(List<Interceptor<? extends Message>> interceptors) {
//		sf.setOutFaultInterceptors(interceptors);
//	}
	public void setProviders(List<? extends Object> providers) {
		sf.setProviders(providers);
	}
	public void setExtensionMappings(Map<Object, Object> extMaps) {
		sf.setExtensionMappings(extMaps);
	}

	public void setAddress(String url) throws ClassNotFoundException
	{
		sf.setAddress(url);
	}

	public void creat()
	{
		sf.create();
	}

	public void destroy()
	{
		if (sf.getServer().isStarted())
		{
			sf.getBus().shutdown(true);
			sf.getServer().destroy();
		}
	}

	@Override
	public void start()
	{
		if (!sf.getServer().isStarted())
		{
			sf.getServer().start();
		}
	}

	@Override
	public void stop()
	{
		if (sf.getServer().isStarted())
		{
			sf.getServer().stop();
		}
	}

	@Override
	public Boolean isStarted()
	{
		return sf.getServer().isStarted();

	}

}
